<!-- CellComponents/CellCheckbox.vue -->
<template>
  <div class="cell-checkbox-wrapper">
    <input
        type="checkbox"
        :checked="isChecked"
        @change="handleChange"
        @focus="$emit('focus')"
        @blur="$emit('blur')"
    />
  </div>
</template>

<script>
import { computed } from 'vue';

export default {
  name: 'CellCheckbox',
  props: {
    modelValue: {
      type: [Boolean, Number, String],
      default: false
    },
    valueMapping: {
      type: Object,
      default: () => ({
        source: "1;0",
        checkbox: "true;false"
      })
    }
  },
  emits: ['update:modelValue', 'focus', 'blur', 'input'],
  setup(props, { emit }) {
    const parseMapping = (mapping) => {
      const [truthy, falsy] = mapping.split(';');
      return { truthy, falsy };
    };

    const sourceMapping = computed(() => parseMapping(props.valueMapping.source));

    const isChecked = computed(() => {
      return props.modelValue.toString() === sourceMapping.value.truthy;
    });

    const handleChange = (event) => {
      const isChecked = event.target.checked;
      const newValue = isChecked ? sourceMapping.value.truthy : sourceMapping.value.falsy;
      emit('update:modelValue', newValue);
      emit('input', newValue);
    };

    return {
      isChecked,
      handleChange
    };
  }
}
</script>

<style scoped>
.cell-checkbox-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

input[type="checkbox"] {
  width: 14px;
  height: 14px;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border: 1px solid #ccc;
  border-radius: 3px;
  outline: none;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
}

input[type="checkbox"]:checked {
  background-color: #4CAF50;
  border-color: #4CAF50;
}

input[type="checkbox"]:checked::before {
  content: '\2714';
  display: block;
  text-align: center;
  font-size: 10px;
  color: white;
  line-height: 14px;
}

input[type="checkbox"]:hover {
  border-color: #999;
}

input[type="checkbox"]:focus {
  box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
}
</style>
